package com.avic.rong.search.adapter.searchResultFragmentsAdapters;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.avic.rong.R;
import com.avic.rong.activity.product.DetailProductActivity;
import com.avic.rong.goodsSort.bean.showRvBean.GoodsSortSearchAllBzBean;
import com.avic.rong.search.activity.SearchHomePageActivity;
import com.avic.rong.wedgit.StringUtil;

import java.util.List;

/**
 * Created by Chenjiaxin on 2019/7/20 12:55
 * E-Mail Address：16601203965@163.com
 * 收缩版adapter
 */
public class SearchVpBzRvAdapter extends RecyclerView.Adapter<SearchVpBzRvAdapter.SearchPeiXunRvViewHolder> {

    private Context context;
    private LayoutInflater inflater;
    private List<GoodsSortSearchAllBzBean.DataBean> list;
    private SearchHomePageBzListener listener;

    public SearchHomePageBzListener getListener() {
        return listener;
    }

    public void setListener(SearchHomePageBzListener listener) {
        this.listener = listener;
    }

    private static int itemCount = 3;

    public void addItemCount(int num) {
        itemCount = num;
    }

    public SearchVpBzRvAdapter(Context context, List<GoodsSortSearchAllBzBean.DataBean> list) {
        this.context = context;
        this.list = list;
        this.inflater = LayoutInflater.from(context);
    }

    @NonNull
    @Override
    public SearchPeiXunRvViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = inflater.inflate(R.layout.search_result_peixun_item, viewGroup, false);
        return new SearchPeiXunRvViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull SearchPeiXunRvViewHolder holder, int i) {

        String title = list.get(i).getTitle();
        String title1 = title.substring(list.get(i).getNo().length());
        if (!StringUtil.isEmpty(title1)) {
            holder.title.setText(title1);
        } else {
            holder.title.setText(list.get(i).getTitle());
        }

        //设置标题筛选高亮
        if (list != null) {
            SpannableString spannableString = new SpannableString(list.get(i).getNo());
            if (SearchHomePageActivity.nowSearchStr != null) {
                int startIndex = list.get(i).getNo().toLowerCase().indexOf(SearchHomePageActivity.nowSearchStr.toLowerCase());
                if (startIndex >= 0) {
                    ForegroundColorSpan colorSpan = new ForegroundColorSpan(ContextCompat.getColor(context, R.color.color_f23030));
                    spannableString.setSpan(colorSpan, startIndex, startIndex + SearchHomePageActivity.nowSearchStr.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
                    holder.no.setText(spannableString);
                } else {
                    holder.no.setText(list.get(i).getNo());
                }
            }
        }

        String time = list.get(i).getPostTime();
        String newTiem = time.substring(0, time.indexOf("T"));
        if (list.get(i)==null){
            holder.type.setVisibility(View.GONE);
        }else{
            String attrShow = list.get(i).getAttrShow();
            if (!StringUtil.isEmpty(attrShow)) {
                holder.type.setText(list.get(i).getAttrShow());
            }else{
                holder.type.setVisibility(View.GONE);
            }

        }
        holder.fabudata.setText("发布日期:" + newTiem);
        holder.shishidata.setText("实施日期:" + newTiem);
        holder.price.setText("¥" + list.get(i).getPrice());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = list.get(i).getUrl();
                String[] split = s.split("/");
                Log.e("cjx", "URL：" + split[2]);
                context.startActivity(DetailProductActivity.getIntent(context, split[2]));
            }
        });
        //添加购物车
        holder.car.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = list.get(i).getUrl();
                String[] split = s.split("/");
                Log.e("cjx", "splite:" + split[2]);
                String skuStr = "{" + "\"" + split[2].toString() + "\"" + ":" + "1}";
                listener.searchHomePageBiaoZhunBzAddCar(skuStr);
            }
        });
    }

    @Override
    public int getItemCount() {
//        if (list.size() > 3)
//            return itemCount;
//        else
        return list.size();
    }

    class SearchPeiXunRvViewHolder extends RecyclerView.ViewHolder {
        TextView type, no, price, title, fabudata, shishidata;
        ImageView car;

        public SearchPeiXunRvViewHolder(@NonNull View itemView) {
            super(itemView);
            type = itemView.findViewById(R.id.view_goods_Bz_TypeName);
            no = itemView.findViewById(R.id.view_goods_Bz_No);
            price = itemView.findViewById(R.id.view_goods_Bz_Price);
            title = itemView.findViewById(R.id.view_goods_Bz_Desc);
            fabudata = itemView.findViewById(R.id.view_goods_Bz_FaBuData);
            shishidata = itemView.findViewById(R.id.view_goods_Bz_ShiShiData);
            car = itemView.findViewById(R.id.view_goods_Bz_Car);
        }
    }

    public interface SearchHomePageBzListener {
        void searchHomePageBiaoZhunBzAddCar(String skuStr);
    }
}
